<?php

/*
 * Tomato Framework
 * 
 * Copyright 2012-2012 Luax.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @link http://www.tomatoframework.com
 * @link http://www.luaxlou.com
 * @version $Id: dao.class.tpl 49 2012-11-05 15:10:18Z luaxcn $
 */

/**
 * 
 * 
 * @package tomatoapp
 * @author Luax <luaxcn@gmail.com>
 */
class <~$resourceName|ucfirst~>Dao {

    public static $pk = '<~$pk~>';

    public static $modifyfields = '<~$s_modifyFields~>';
    public static $showfields = '<~$s_showFields~>';
    public static $listfields = '<~$s_listFields~>';

    public static $columns = array(
        <~foreach from="$columns" item="col" ~>
            '<~$col.name~>' => '<~$col.type~>',
        <~/foreach~>
    );
    
    public static $autoCreates = array(
        <~foreach from="$autoCreates" item="v" key="k"~>
            '<~$k~>' => '<~$v~>',
        <~/foreach~>
    );
    
    public static $autoUpdates = array(
        <~foreach from="$autoUpdates" item="v" key="k" ~>
            '<~$k~>' => '<~$v~>',
        <~/foreach~>
    );

    public function findAll() {
        $keys = self::$listfields;
        $sql = "select 
                    <~$pk~>,
                    <~$caption~>,
                    {$keys}
                from <~$tableName~>
                limit 10000
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute();

        return $sth->fetchAll(PDO::FETCH_ASSOC);
    }

    public function find($id) {
        $keys = self::$showfields;

        $sql = "select 
                    <~$pk~>,
                    {$keys} 
                from <~$tableName~>
                where <~$pk~> = ?
                limit 1
            ";

        $sth = Load::db()->prepare($sql);

        $sth->execute(array($id));

        return $sth->fetch(PDO::FETCH_ASSOC);
    }

    public function save($<~$resourceName~>) {
     

        $keys = self::$modifyfields;

        $values = implode(',',array_fill(0, sizeof(explode(',',$keys)), '?'));
        
        $data = array_values(SqlTemplate::filterFields($keys, $<~$resourceName~>));
        
        
        if(sizeof(self::$autoCreates)>0)
        {
            $keys = $keys.','.implode(',',array_keys(self::$autoCreates));
            
            $values = $values.','.implode(',',array_values(self::$autoCreates));
        }

        $sql = "insert into <~$tableName~>
                (
                    $keys
                )
                values
                (
                    $values
                )
            ";
       
        $db = Load::db();
        $sth = $db->prepare($sql);

         $result = $sth->execute($data);

 

        if ($result) {
            
            $lastInsertId = Load::db()->lastInsertId();
           
            return $lastInsertId;
        }
        
        return FALSE ;
    }

    public function update($<~$resourceName~>) {

        $t = SqlTemplate::updatePairs(self::$modifyfields, ':');

        
        
        if(sizeof(self::$autoUpdates)>0)
        {
            $arr = array();
            foreach(self::$autoUpdates as $k=>$v)
            {
            
                $arr[] = "{$k}={$v}";
                
             }
           $t = $t.','.implode(',',$arr);      
             
        }
        
        $sql = "update <~$tableName~> set
                $t
                where <~$pk~> = :<~$pk~>
                limit 1
            ";
              
        $sth = Load::db()->prepare($sql);

        return $sth->execute($<~$resourceName~>);
    }

    public function delete($id) {
        $sql = "delete 
                from <~$tableName~>
                where <~$pk~> = ?
                limit 1
            ";
        $sth = Load::db()->prepare($sql);

        return $sth->execute(array($id));
    }

}

?>